﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="news_column_tree.aspx.cs"
    Inherits="Ccit.OA.NewsCenter.news_column_tree" %>
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>
<%@ Register Assembly="Microsoft.Web.UI.WebControls" Namespace="Microsoft.Web.UI.WebControls"
    TagPrefix="iewc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>信息中心-栏目树</title>

    <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>

    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript"></script>

    <script language="javascript" src="../script/jsonlib.js" type="text/javascript"></script>

    <script language="javascript" src="../script/jcontrol.js" type="text/javascript"></script>

    <style type="text/css">
        .skin
        {
            border-right: buttonhighlight 2px outset;
            border-top: buttonhighlight 2px outset;
            font-size: 9pt;
            visibility: hidden;
            border-left: buttonhighlight 2px outset;
            width: 100px;
            cursor: default;
            border-bottom: buttonhighlight 2px outset;
            font-family: Arial, Helvetica, sans-serif;
            position: absolute;
            background-color: menu;
            text-align: left;
        }
        .menuitems
        {
            padding-right: 5px;
            padding-left: 5px;
            padding-top: 5px;
            padding-bottom: 5px;
        }
    </style>
</head>
<body onclick="hideMenu()">
    <ccit:ActionForm id="form1" runat="server">
    <input type="hidden" id="_hidTabId" runat="server" />
    <input type="hidden" id="_hidWhereClause" runat="server" />
    <!--满足权限范围的栏目ids-->
    <input type="hidden" id="_hidRangColIds" runat="server" />
    <!--增加时返回的下拉父节点的id-->
    <input type="hidden" value="" id="_hidUpTypeId" runat="server" />
    <!--点击树自带的节点id-->
    <input type="hidden" id="_hidOriUpTypeId" runat="server" />
    <!--是否展开-->
    <input type="hidden" id="_hidAddNewSign" runat="server" />
    <!--保存后传来的主键id-->
    <input type="hidden" id="_hidNewColumnId" runat="server" />
    <input type="hidden" id="_hidExpandSign" runat="server" />
    <input type="hidden" id="_hidLevelStruct" runat="server" />
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>
    <!--传入参数类型(树)-->
    <input type="hidden" id="hdType" runat="server" value="" />
    
    <div id="root" class="skin" style="left: 0px; visibility: hidden; width: 110px; top: 0px;">
        <div onclick="clickItem()" onmouseover="highlighItem()" onmouseout="lowlightItem()" style="border-bottom: window 2px inset;" class="menuitems" func="AddColumnType">
            <img src="../images/toolbar/addnew_bq.gif" border="0" />&nbsp; 新子类别
        </div>
        <div onclick="clickItem()" onmouseover="highlighItem()" onmouseout="lowlightItem()" class="menuitems" func="DeleteColumnType">
            <img src="../images/toolbar/del_bq.gif" border="0" />&nbsp; 删除类别
        </div>
    </div>
    <iewc:TreeView ID="tvColumn" runat="server">
    </iewc:TreeView>
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display: none" value="触发事件" />

    <script language="javascript" type="text/javascript">
        var menuskin = "skin";
	    var node = null;
	    var menuname = "root";		
		
		function hideMenu() 
		{		    
			var obj = document.getElementById(menuname);
			if (null != obj && typeof(obj) != "undefined")
				document.getElementById(menuname).style.visibility = "hidden";
		}
        function highlighItem() 
		{    
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "highlight";
				event.srcElement.style.color = "white";
			}
		}
		function lowlightItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "";
				event.srcElement.style.color = "black";
				window.status = "";
			}
		}
		//新增，删除
		function clickItem() 
		{								
			if(node != null)
			{
				var vNodeId = node.getAttribute("ID");
				var nodeType = node.getAttribute("type").toUpperCase();		
								
				var vNodeIdArray = vNodeId.split("_");
				var columnId = vNodeIdArray[0];
												
				if (event.srcElement.className == "menuitems" && event.srcElement.disabled != true) 
				{
					var sFuncName = event.srcElement.getAttribute("func");					
					switch (sFuncName)
					{
						case "AddColumnType":	//addnew sub asset type
						 //   alert(columnId);
						    var type=$get("hdType").value;
						    var vPageParam = "columnId=" + columnId+"&Type="+type+"&oper=0&TABID="+document.getElementById("_hidTabId").value;
						    var encodeParam = base64encode(vPageParam);
							parent.frames[1].location.href = "./news_column_edit.aspx?" + encodeParam;
							break;
						case "DeleteColumnType": //delete asset type						    
						    if(!confirm("删除当前类别连同该类别下的信息一同删除,是否继续?"))
                                return;
                            var nodeIndex = node.getNodeIndex();       
                            PageMethods.DeleteColumn(columnId,nodeIndex,ClientCallBack);						    
							break;
						default:
							break;
					}						
				}
			}
		}
		function tvColumn.onexpand()
		{	
		    /* Step 1：find current click TreeNode */	    
		    var nodeIndex = event.treeNodeIndex;	
		   // alert(nodeIndex);		
			if (typeof(nodeIndex) == "undefined"){			
				node = null;
				return false;
			}
			
			tvColumn.selectedNodeIndex = nodeIndex;
			node = tvColumn.getTreeNode(nodeIndex);
						
			/* Step 2: get asset type id，and get all child types */			
			var vNodeId = node.getAttribute("ID");
			var vNodeIdArray = vNodeId.split("_")
			var vColumnId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];
			if(vColumnId != "0" && vLoadSign == "0") {
			    var oSubNodes = node.getChildren();
			    var oSubNode;
			    for(var i = oSubNodes.length - 1; i >= 0; i --) {
			        oSubNode = oSubNodes[i];
			        oSubNode.remove();
			    }
			    var expandSign = $get("_hidExpandSign").value;   //是否逐级展开 1：是
			    var levelStruct = $get("_hidLevelStruct").value;
			  //  var Type = document.getElementById("hdType").value;
			    PageMethods.GetSubTypesByColumnId(vColumnId,nodeIndex,expandSign,levelStruct,ClientCallBack);
			}
			hideMenu();
		}
		
		function tvColumn.oncollapse()
		{
			hideMenu();
		}
		
		function tvColumn.oncontextmenu()
		{				
			hideMenu();
			var nodeIndex = event.treeNodeIndex;							
			if (typeof(nodeIndex) == "undefined")
			{
				node = null;
				return false;
			}
			tvColumn.selectedNodeIndex = nodeIndex;
			node = tvColumn.getTreeNode(nodeIndex);	
		    var color =node.getAttribute("DefaultStyle");		
			var vNodeId = node.getAttribute("ID");			
			var vNodeIdArray = vNodeId.split("_");
			var vDeptId = vNodeIdArray[0];
			
			if(vDeptId == "0") return;  //根节点不显示右键菜单
			if(color == "color:red;") return ;  //不能编辑的时候右键菜单不可用
			
			var obj = document.getElementById("root");				
			if (null == obj || typeof(obj) == "undefined")
			{
				node = null;
				return false;
			}
			
			var rightedge = document.body.clientWidth - event.clientX;		//右边距
			var bottomedge = document.body.clientHeight - event.clientY;	//底边距
			
			if (rightedge < obj.offsetWidth)
				obj.style.left = document.body.scrollLeft + 5 + event.clientX - obj.offsetWidth;
			else
				obj.style.left = document.body.scrollLeft + 5 + event.clientX ;
			
			if (bottomedge < obj.offsetHeight)
				obj.style.top = document.body.scrollTop + 5 + event.clientY - obj.offsetHeight ;
			else
				obj.style.top = document.body.scrollTop + 5 + event.clientY ;
											
			obj.style.visibility = "visible";
			return false;
		}
		//返回结果
		function ClientCallBack(vResult) {	
		     var vNodeIdArray = vResult.split("_")
		     var vOperType=vNodeIdArray[0];
		     switch(vOperType) {		       
                case "GetSubTypesByColumnId"://获取子节点
                         var vExeResult = vNodeIdArray[1];
			             var nodeIndex = vNodeIdArray[2];
			             var expandSign = vNodeIdArray[3];
			             var levelStruct = vNodeIdArray[4];
			             var parentNodeId = vNodeIdArray[5];
            			 
		                eval("var objResult="+vExeResult); 
                        for(var i=0;i<objResult.length;i++)
                        {
                           var oResult=objResult[i];
                           createSubTreeNodes(nodeIndex,oResult);//展开老祖宗
                        }         
                        
                        if(expandSign == "1") {//是逐级展开
                            var parentIdArr = levelStruct.split(",");
                            var iLen = parentIdArr.length;
                            var bGet = false;
                            for(var i = 0; i < iLen; i ++) 
                            {                                    
                                var colId = parentIdArr[i];     
                                if(!bGet) 
                                {
                                    if(colId == parentNodeId)
                                     {
                                        if(i == (iLen - 1)) 
                                        {//如果到最底层，则逐级展开标志0
                                            $get("_hidExpandSign").value = "0";                                
                                        }
                                        bGet = true;
                                        continue;
                                    }
                                }
                                else {//true，展开下级
                                    var ctrlNode = findTreeNode(colId);
                                    var nodeAttrId = ctrlNode.getAttribute("ID");    
                                    var vNodeIdArray = nodeAttrId.split("_");
			                        if(vNodeIdArray[1] == "1") {
			                            if(i == (iLen - 1)) {
                                            ctrlNode.setAttribute("EXPANDED",true);                               
                                        }
                                        else
			                                continue;
			                        }
			                       else {
			                            ctrlNode.setAttribute("EXPANDED",true);
			                            break;
			                        }
                                }
                            }
                        } 
                       break;   
                case "DeleteColumn"://删除
                     var oResult=vNodeIdArray[1];
                      eval("var oResultObj="+oResult);
		               nodeIndex =oResultObj.onodeIndex;   
                       alert(oResultObj.oMes);
                        if(parseInt(oResultObj.ores) > 0) {
                            var node = $get("tvColumn").getTreeNode(nodeIndex); 
                            node.remove();
                          }
                          var vPageParam = "Type="+$get("hdType").value+"&oper=0&TABID="+document.getElementById("_hidTabId").value;
                          var encodeParam = base64encode(vPageParam);
                          parent.frames[1].document.location="news_column_edit.aspx?" + encodeParam;
                          break;
                case "GetNodeLevelStruct":
                     var vResult=vNodeIdArray[1];
                     eval("var objResult="+vResult); 
		             var levelStruct = objResult.resultIds;
		             $get("_hidLevelStruct").value = levelStruct;
                     
                     var parentIdArr = levelStruct.split(",");
                     var iLen = parentIdArr.length;
                     var colId = "";
                     var ctrlNode;
                     var nodeAttrId = "";
                     for(var i = 0; i < iLen ; i ++) {
                        colId = parentIdArr[i];
                        ctrlNode = findTreeNode(colId);
                        nodeAttrId = ctrlNode.getAttribute("ID");
                                        
		                var vNodeIdArray = nodeAttrId.split("_")
			            if(vNodeIdArray[1] == "1") { //判断最老祖宗是否展开过 1--：展开过
			                continue; 
			            }
			            else {
			                ctrlNode.setAttribute("EXPANDED",true);//没展开连续展开调tvColumn.onexpand()方法展开最老祖宗
			                break;
			            }
                     } 
                         objResult = null;     
                         break;
                 }
          }
        
        function contain(vRangeCol,colId)
        {
            var bFind = false;
            if(vRangeCol != "")
            {
                var ArrRange = vRangeCol.split(",");
                for(var i = 0;i < ArrRange.length;i++)
                {
                    if(colId == ArrRange[i])
                      bFind = true;
                }   
            }
            return bFind;
        }
        
        //追加子节点
        function createSubTreeNodes(nodeIndex,oResult) { 
                 var isAddNew = $get("_hidAddNewSign").value; //isAddNew = 1 时编辑
                 var vRangeColIds = $get("_hidRangColIds").value;
			     var node = $get("tvColumn").getTreeNode(nodeIndex); 
			     
			     var bFind = contain(vRangeColIds,oResult.columnId);
			     
			     var vType = document.getElementById("hdType").value;
			     var vPageParam = "columnId=" + oResult.columnId+"&oper=1&Type="+vType+"&TABID="+document.getElementById("_hidTabId").value;;
                 var encodeParam = base64encode(vPageParam);
                 vNavigateUrl = "./news_column_edit.aspx?" + encodeParam;
                 
                 oSubNode = $get("tvColumn").createTreeNode();
                 if(isAddNew!=""&&isAddNew == 0) //新增
                 {
                     oSubNode.setAttribute("Text",oResult.columnName);
                     oSubNode.setAttribute("NavigateUrl",vNavigateUrl);
                     
                 }else
                 {
                    if(bFind == true)
                    {
                      oSubNode.setAttribute("Text",oResult.columnName);
                      oSubNode.setAttribute("NavigateUrl",vNavigateUrl);
                    }else
                    {
                       oSubNode.setAttribute("Text",oResult.columnName);
                       oSubNode.setAttribute("NavigateUrl","");
                       oSubNode.setAttribute("DefaultStyle","color:red;");
                    }
                 }
                 
		         oSubNode.setAttribute("ID",oResult.columnId + "_" + "0");
		         oSubNode.setAttribute("Type","root");
		         oSubNode.setAttribute("Target","iFrmEdit");
		         oSubNode.setAttribute("ImageUrl","../images/toolbar/filelogo.gif");
		         
		         
		         if(oResult.hasChild == "1") {  //if contain sub node then add empty node
		           oEmptyNode = $get("tvColumn").createTreeNode();  
		           oSubNode.add(oEmptyNode); 
		         }
		         node.add(oSubNode);
		        /* set node load sign = 1 */
		        var vNodeId = node.getAttribute("ID");
		        var vNodeIdArray = vNodeId.split("_")
			    oResult.columnId = vNodeIdArray[0];
			    node.setAttribute("ID",oResult.columnId  + "_" + "1");		    
        }
        
        //返回新父节点对象
        function findTreeNode(findAstTypeId) {
            var ctrlTree = document.getElementById("tvColumn");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); //获取根节点
		    var ctrlChildNodes = ctrlRoot.getChildren();  //所有子节点
	    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,findAstTypeId);
            return ctrlFindNode;
        }
        //循环查找新父节点，返回新父节点对象
        function recursionFind(ctrlChildNodes,findAstTypeId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vAstTypeId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vAstTypeId = vNodeIdArray[0];
		        if(vAstTypeId == findAstTypeId){		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,findAstTypeId);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        
        function doTrigger() {
            doOperOriParentNode();//找原父节点
            doOperDtParentNode();//找新父节点
		}
		//找原父节点
		function doOperOriParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpTypeId").value; //原父节点的主键(绑定到树节点的主键)
		    var vUpTypeId = $get("_hidUpTypeId").value;		 //新父节点的主键(绑定到树节点的主键)
		    var isAddNew = $get("_hidAddNewSign").value;   //是否为编辑 1:编辑
		    
		        
		    if(isAddNew=="1"){//编辑
		        if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
		            var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		            eval("var curNodeObj=" + curNodeExpr);
		            var colName = curNodeObj.columnName;
		            var ctrClickObj = findTreeNode(curNodeObj.columnId);
		            ctrClickObj.setAttribute("Text",colName);
		            return;
		        }
		        /* 初始化全局变量 */
		        ctrlOgNode = null;		    
		        vOgNodeIndex = "";
		        // get root node object
		        var ctrlTree = document.getElementById("tvColumn");	
		        var ctrlRoot = ctrlTree.getTreeNode("0");
    		    
		        /* 获取原始上级节点对象 */
		        //根据原父节点主键获取其树的节点索引(eg：根据点击树主键3，获取其树的索引0.1)
		        if(vOriUpTypeId != ""&&vOriUpTypeId!="0") {
		            ctrlOgNode = findTreeNode(vOriUpTypeId);  //根据原父节点主键获取其树的节点对象
		            vOgNodeIndex = ctrlOgNode.getNodeIndex();  //根据节点对象获取树节点索引eg:0.0.1
		        }
		        else {   //无上级父节点时
		            ctrlOgNode=ctrlRoot;  
		            vOgNodeIndex= "0";
		        }
		        var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		        eval("var curNodeObj=" + curNodeExpr);
                // find current node position index
                var curNode = findTreeNode(curNodeObj.columnId);  //获取当前编辑节点对象对应的树节点对象
                
                // delete origin node
                curNode.remove();  //移除
                
                curNodeObj = null;
		    }
		    return;		    
		}
		//找新父节点
		function doOperDtParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpTypeId").value;  
		    var vUpTypeId = $get("_hidUpTypeId").value;		//新的上级节点    
		    var isAddNew = $get("_hidAddNewSign").value; //isAddNew = 1 时为编辑
		    
		    /* 初始化全局变量 */
		    
		    ctrlDtNode = null;		    
		    vDtNodeIndex = "";
		    
		    // get root node object
		    var ctrlTree = document.getElementById("tvColumn");	
		    var ctrlRoot = ctrlTree.getTreeNode("0");
		    
		    
		    /* 获取新上级节点对象 */
		    if(vUpTypeId != ""&& vUpTypeId!="0") {		        
		        ctrlDtNode = findTreeNode(vUpTypeId);	  //新上级节点对象	
		        if(null == ctrlDtNode) {    //找不到新父节点
		            expandDtNode(vUpTypeId);
		            return;
		        }
		        getParentNode(ctrlDtNode);
		        vDtNodeIndex = ctrlDtNode.getNodeIndex();	
		        
		        var ctrlSubChildNodes = ctrlDtNode.getChildren();
		        var iChildLength = ctrlSubChildNodes.length;
		        
		        var bHasChild = false;
		        if(iChildLength > 0)
		           bHasChild = true;		    
		           
		        var ArrayID = ctrlDtNode.getAttribute("ID").split("_");
		        if(bHasChild == true){
		           if(ArrayID[1]=="0"){ //ArrayID[1]=="0"未展开 ;bHasChild != null 有子节点
		             ctrlDtNode.setAttribute("EXPANDED",true);
		             return ;
		          }
		       }
		        
		    }
		    else {	
		        ctrlDtNode=ctrlRoot;	
		        vDtNodeIndex= "0";  
		    }
		    
		    var curNodeExpr = $get("_hidNewColumnId").value;
		    eval("var curNodeObj=" + curNodeExpr);
		    
		    if(isAddNew=="1"){//编辑
	            if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
	                return;
	            }
		        else {		           
		            // add node to destination parent node	
		              createSubTreeNodes(vDtNodeIndex,curNodeObj);
		        }   
		    }
		    else //新增
		    {		       	                
		       createSubTreeNodes(vDtNodeIndex,curNodeObj);
		    }
		    
		    ctrlDtNode.setAttribute("EXPANDED",true);
		    curNodeObj = null;
		}
		
		//获取上级父节点,将其展开
		function getParentNode(dtNode)
		{
		   var parNode = dtNode.getParent();
		   parNode.setAttribute("EXPANDED",true);
		   if(parNode.getNodeIndex() != "0")
		      getParentNode(parNode);
		}
		
		function expandDtNode(dtNodeId) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,ClientCallBack);  //获取新父节点上级节点
		}
		
		
		function initialTreeNode(ctrlFindNode) {
		    var ctrlTree = document.getElementById("tvColumn");	
		    var vNodeId;
		    var vNodeIdArray;
		    var vAstTypeId;	
		    var vNodeIndex;
		    if(null != ctrlFindNode) {
		        vNodeId = ctrlFindNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vAstTypeId = vNodeIdArray[0];
		        ctrlFindNode.setAttribute("ID",vAstTypeId + "_" + "0");
		        vNodeIndex = ctrlFindNode.getNodeIndex();
		        ctrlTree.selectedNodeIndex = vNodeIndex;
		        /* 如果当前节点没有下级节点，则不会触发expand事件,
		         * 此时需要在当前节点下增加一个空节点 */
		        var vChildNodes = ctrlFindNode.getChildren();
		        if(vChildNodes.length == 0) {
		           var oEmptyNode = $get("tvColumn").createTreeNode();  
		           ctrlFindNode.add(oEmptyNode);
		        }
		        ctrlFindNode.setAttribute("EXPANDED",true);
		    }
		}
    </script>

    </ccit:ActionForm>
</body>
</html>
